package com.sduept.nwld.kpi.controller;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.nwld.kpi.datasource.KpiDataSource;
import com.sduept.nwld.kpi.manager.KpiManager;
import com.sduept.nwld.kpi.model.KpiLevel;
import com.sduept.nwld.kpi.model.KpiNormal;
import com.sduept.nwld.kpi.model.KpiWeight;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Named
@ViewScoped
public class KpiSourceViewController {
	@Autowired
	private KpiManager km;
	@Autowired
	private SuperDAO dao;
	
	private TreeNode root;
	private TreeNode selectedNode;
	private String selectedView;
	private boolean realiIsShow = true;
	
	private List<KpiDataSource> sources = new ArrayList<>();
	private Map<String,KpiLevel> levelsmap = new HashMap<>();
	private Map<String,KpiNormal> kpismap = new HashMap<>();
	
	private Map<String,List<KpiNormal>> dsKpiMap = new HashMap<>();
	
	@PostConstruct
	public void init(){
		List<KpiLevel> levels = dao.findAll(KpiLevel.class);
		List<KpiNormal> kpis = dao.findAll(KpiNormal.class);
		List<KpiWeight> weights = dao.findAll(KpiWeight.class);
		sources = dao.findAll(KpiDataSource.class);

		kpismap.clear();
		for(KpiNormal n:kpis){
			kpismap.put(n.getId(), n);
		}
		
		dsKpiMap.clear();
		for(KpiWeight w : weights){
			if(w.getCtype().equals("kpidatasource")){
				List<KpiNormal> ks = dsKpiMap.get(w.getCid());
				if(ks == null){
					ks = new ArrayList<KpiNormal>();
					dsKpiMap.put(w.getCid(), ks);
				}
				ks.add(kpismap.get(w.getPid()));
			}
		}
		
		//冒泡排序，被引用次数多的排前面
		for (int i = 0; i < sources.size()-1; i++){
			int size1 = 0;
			List<KpiNormal> tmps1 = dsKpiMap.get(sources.get(i).getId());
			if(tmps1 != null){
				size1 = tmps1.size();
			}
			for(int j=i+1;j<sources.size();j++){
				int size2 = 0;
				List<KpiNormal> tmps2 = dsKpiMap.get(sources.get(j).getId());
				if(tmps2 != null){
					size2 = tmps2.size();
				}
				if(size1 < size2){
					KpiDataSource temp = sources.get(i);
					sources.set(i, sources.get(j));
					sources.set(j, temp);
				}
			}
		}
		for(KpiLevel l:levels){
			levelsmap.put(l.getId(), l);
		}
		root = new LibTreeNode("root","数据源", null);
		loadChildren(root);
	}
	public void loadChildren(TreeNode node){
		for(KpiDataSource ds:sources){
			List<KpiNormal> ks = dsKpiMap.get(ds.getId());
			if(ks == null){
				DSWithKpiImportSize dsWithSize = new DSWithKpiImportSize(ds,0);
				if(ds.isAvailable() && ds.getReliabilityParam() > 0){
					new DefaultTreeNode("dsSizeCorrect",dsWithSize,node);
				}else{
					new DefaultTreeNode("dsSizeError",dsWithSize,node);
				}
			}else{
				TreeNode source = null;
				DSWithKpiImportSize dsWithSize = new DSWithKpiImportSize(ds,ks.size());
				if(ds.isAvailable() && ds.getReliabilityParam() > 0){
					source = new LibTreeNode("dsSizeCorrect",dsWithSize,node);
				}else{
					source = new LibTreeNode("dsSizeError",dsWithSize,node);
				}
				loadDataSourceChildren(source);
			}
		}
		node.setExpanded(true);
	}
	public void loadDataSourceChildren(TreeNode node){
		KpiDataSource ds = (KpiDataSource)( (DSWithKpiImportSize)node.getData()).getDs();
		for(KpiNormal n : dsKpiMap.get(ds.getId())){
			int levelvalue = 0;
			if(n.getKpiLevel() != null){
				levelvalue = levelsmap.get(n.getKpiLevel()).getValue();
			}
			if(n.getReliabilityValue() >= levelvalue){
				new DefaultTreeNode("kpiCorrect",n,node);
			}else{
				new DefaultTreeNode("kpiError",n,node);
			}
		}
		node.setExpanded(true);
	}
	public class DSWithKpiImportSize{
		public DSWithKpiImportSize(KpiDataSource ds,int size){
			this.ds = ds;
			this.size = size;
		}
		public DSWithKpiImportSize(){}
		private KpiDataSource ds;
		private int size = 0;
		public KpiDataSource getDs() {
			return ds;
		}
		public void setDs(KpiDataSource ds) {
			this.ds = ds;
		}
		public int getSize() {
			return size;
		}
		public void setSize(int size) {
			this.size = size;
		}
	}
	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public String getSelectedView() {
		return selectedView;
	}

	public void setSelectedView(String selectedView) {
		this.selectedView = selectedView;
	}
	public boolean isRealiIsShow() {
		return realiIsShow;
	}
	public void setRealiIsShow(boolean realiIsShow) {
		this.realiIsShow = realiIsShow;
	}

}
